package cc.web.vaaking.servlet.user;

import cc.web.vaaking.common.AjaxResult;
import cc.web.vaaking.common.AjaxTool;
import cc.web.vaaking.common.utils.ValueUtils;
import cc.web.vaaking.entity.SysUser;
import cc.web.vaaking.services.SysRoleService;
import cc.web.vaaking.services.SysUserService;
import cc.web.vaaking.servlet.BaseHttpServlet;
import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseHttpServlet {
    SysUserService sysUserService = new SysUserService();
    SysRoleService sysRoleService = new SysRoleService();

    @Override
    public void init() throws ServletException {

        routerMap.put("addPage", this::addPage);
        routerMap.put("update", this::update);
        routerMap.put("editPagee", this::editPag);
        routerMap.put("addUser", this::addUser);
        routerMap.put("delUser", this::delUser);
        routerMap.put("updateRolePage", this::updateRolePage);//更新用户职位
        routerMap.put("updateRole", this::updateRole);//更新用户职位
    }

    private void updateRolePage(HttpServletRequest request, HttpServletResponse response) {
        try {
            int userId = Integer.parseInt(ValueUtils.getParamOrDefault(request, "userId", "-1"));
            SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
            request.setAttribute("userinfo", JSON.toJSONString(sysUser));
            request.getRequestDispatcher("/views/user/updateRole.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void updateRole(HttpServletRequest request, HttpServletResponse response) {
        try {
            SysUser sysUser = AjaxTool.getJSON(request).toJavaObject(SysUser.class);
            sysRoleService.updateUserRoleByUserId(sysUser.getId(), sysUser.getSysRole().getId());
            AjaxTool.returnAjax(response, AjaxResult.success("修改成功"));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }

    }

    private void delUser(HttpServletRequest request, HttpServletResponse response) {
        try {
            SysUser sysUser = AjaxTool.getJSON(request).toJavaObject(SysUser.class);
            sysUserService.deleteByPrimaryKey(sysUser.getId());
            AjaxTool.returnAjax(response, AjaxResult.success("成功"));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }
    }


    private void editPag(HttpServletRequest request, HttpServletResponse response) {
        try {
            int userId = Integer.parseInt(ValueUtils.getParamOrDefault(request, "userId", "-1"));
            SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
            request.setAttribute("userinfo", JSON.toJSONString(sysUser));
            request.getRequestDispatcher("/views/user/editUser.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) {
        SysUser sysUser = AjaxTool.getJSON(request).toJavaObject(SysUser.class);
        try {
            sysUserService.insert(sysUser);
            AjaxTool.returnAjax(response, AjaxResult.success("成功"));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }


//        if(sysUser.getPassword())
    }

    private void addPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("/views/user/addUser.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        SysUser sysUser = AjaxTool.getJSON(request).toJavaObject(SysUser.class);
        int cnt = sysUserService.updateByPrimaryKeySelective(sysUser);
        if (cnt == 1)
            AjaxTool.returnAjax(response, AjaxResult.success("保存成功"));
        else
            AjaxTool.returnAjax(response, AjaxResult.error("保存失败"));
    }

}
